package com.glory.gloryquickui.adapter

import android.widget.CompoundButton
import android.widget.LinearLayout
import android.widget.RadioButton
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.glory.gloryquickui.utils.FloatingWindowsUtils
import com.glory.gloryquickui.R
import com.glory.gloryquickui.entity.SingleChooseOptionsBean

class RadioButtonSelectAdapter(
    data: List<SingleChooseOptionsBean.Option?>?,
    mCallback: FloatingWindowsUtils.PopTextAndPositionCallback,
    disabled: Boolean,
    border : Boolean
) : BaseQuickAdapter<SingleChooseOptionsBean.Option?, BaseViewHolder>(
    R.layout.item_radio_button,
    data as MutableList<SingleChooseOptionsBean.Option?>?
) {
    private var index = -1 //标记当前选择的选项
    private var mCallback: FloatingWindowsUtils.PopTextAndPositionCallback? = mCallback
    private val mDisabled : Boolean = disabled
    private val mBorder : Boolean = border

    //标记当前选择的选项
    override fun convert(helper: BaseViewHolder, item: SingleChooseOptionsBean.Option?) {
        helper.setText(R.id.tvSelectName, item?.label)
        val rbBtn = helper.getView<RadioButton>(R.id.rbSelect)
        val llRadioLayout = helper.getView<LinearLayout>(R.id.llRadioLayout)
        rbBtn.isEnabled = !mDisabled
        rbBtn.setOnClickListener() {
            if (rbBtn.isChecked) {
                index = helper.adapterPosition
                mCallback?.onSuccess(item?.value.toString(), helper.adapterPosition)
                notifyDataSetChanged()
            }
        }
        rbBtn.isChecked = index == helper.adapterPosition
        if (mBorder){
            llRadioLayout.setBackgroundResource(
                if (index == helper.adapterPosition) R.drawable.shape_blue_line_white_fill_corner12_select
                    else R.drawable.shape_gray_line_white_fill_corner12_select)
        }
    }
}